package com.johann.leetcode;

public class T11_MaxArea {
    // 暴力解法
    public int maxArea(int[] height) {
        if (height.length<=1) {
            return 0;
        }
        int maxTank=0;
        for (int i = 0; i < height.length; i++) {
            for (int j = height.length - 1; j >= i+1; j--) {
                maxTank = Math.max((j - i) * Math.min(height[i], height[j]),maxTank);
            }
        }
        return maxTank;
    }
    // 双指针法，只需要关注是否有将结果推高的可能性
    public int maxArea02(int[] height){
        if (height.length<=1) {
            return 0;
        }
        int lIdx=0;
        int rIdx= height.length-1;
        int tank = 0;
        while (lIdx<rIdx){
            tank = Math.max(tank,(rIdx-lIdx)*Math.min(height[rIdx],height[lIdx]));
            // 如何向中间靠？
            if (height[lIdx]>height[rIdx]) {
                rIdx--;
            }else {
                lIdx++;
            }
        }
        return tank;
    }

    public static void main(String[] args) {
        System.out.println( new T11_MaxArea().maxArea02(new int[]{1,8,6,2,100,100,8,3,7}));
        System.out.println( new T11_MaxArea().maxArea(new int[]{1,1}));


    }
}
